package com.cd.cms.modules.mid.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cd.cms.common.persistence.Page;
import com.cd.cms.common.utils.StringUtils;
import com.cd.cms.common.web.BaseController;
import com.cd.cms.modules.sys.entity.Dict;
import com.cd.cms.modules.sys.service.DictService;

/**
 * 平台Controller
 * @author XueD
 * @version 2016-05-10
 */
@Controller
@RequestMapping(value = "${adminPath}/mid/platform")
public class PlatformController extends BaseController {

	@Autowired
	private DictService dictService;
	
	@ModelAttribute
	public Dict get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return dictService.get(id);
		}else{
			return new Dict();
		}
	}
	
	@RequiresPermissions("mid:platform:view")
	@RequestMapping(value = {"list", ""})
	public String list(Dict dict, HttpServletRequest request, HttpServletResponse response, Model model) {
		dict.setType("sys_user_type");
        Page<Dict> page = dictService.findPage(new Page<Dict>(request, response), dict); 
        model.addAttribute("page", page);
		return "modules/mid/platformList";
	}

	@RequiresPermissions("mid:platform:view")
	@RequestMapping(value = "form")
	public String form(Dict dict, Model model) {
		model.addAttribute("dict", dict);
		return "modules/mid/platformForm";
	}

	@RequiresPermissions("mid:platform:edit")
	@RequestMapping(value = "save")//@Valid 
	public String save(Dict dict, Model model, RedirectAttributes redirectAttributes) {
		dict.setSort(10);
		if (!beanValidator(model, dict)){
			return form(dict, model);
		}
		dict.setType("sys_user_type");
		dictService.save(dict);
		addMessage(redirectAttributes, "保存平台'" + dict.getLabel() + "'成功");
		return "redirect:" + adminPath + "/mid/platform/?repage&type="+dict.getType();
	}
	@RequiresPermissions("mid:platform:edit")
	@RequestMapping(value = "delete")
	public String delete(Dict dict, RedirectAttributes redirectAttributes) {
		dictService.delete(dict);
		addMessage(redirectAttributes, "删除平台成功");
		return "redirect:" + adminPath + "/mid/platform/?repage&type="+dict.getType();
	}
	
	
	
	
}
